# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

pkgbase='libpsl'
pkgname=("${pkgbase}" "${pkgbase}-devel")
pkgver=0.21.0
pkgrel=1
pkgdesc='Public Suffix List library'
url='https://github.com/rockdaboot/libpsl'
arch=('i686' 'x86_64')
license=('MIT')
makedepends=('libxslt' 'python3' 'libidn2-devel' 'publicsuffix-list' 'libunistring-devel')
source=(https://github.com/rockdaboot/libpsl/releases/download/${pkgname}-${pkgver}/${pkgname}-${pkgver}.tar.gz
        0.16.0-install-dafsa.patch 
        0.16.1-make-dafsa-setlocale.patch
        019.1-implement-private-libs.patch
        0.21.0-fix-make-test.patch)
sha512sums=('165c4f0b0640a813d512bd916e1532e32e43c8c81a5efd048f3a5b07b1b3c9129b4c4b5008b8b11a7c1b3914caea17564321389cd350bf1d687d53a97f2afa4d'
            '42cf2d5ff3bd48fbe00d1bf5bc4e7acb0554b9fae65b941c27abf8bd8a21d2c678b78b1e356c7596fc20d17e226dc4014e36b95eb785e9beb41e43d074ef5483'
            '36f4ae0bee8d25a55fb38b64525fa855698830310f5b7e5b3c5c96fa6799622225eab67ccfdb0e20d2092f1398563231c02aecd4bc8c042796cb471ca905fd10'
            '93290b524b7ea0b4aa5abd50a1cdcad4eeb8d135d7c9d6da02a4751065a7a93fd3c12a5b0be399baf00584d5830f7fb59f4414d56cfd139ec493a799ffbee1d6'
            '34a40e36c310dc91a7d333d59ee4d62fd3636c6e1ad92641a91733380755b81b352ff8a0706ef4fe92d2b09b208af0dd566c73e90ebd5bd9b5648efb902c68c2')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${pkgbase}-${pkgver}
  apply_patch_with_msg \
    0.16.0-install-dafsa.patch \
    0.16.1-make-dafsa-setlocale.patch \
    019.1-implement-private-libs.patch \
    0.21.0-fix-make-test.patch

  rm -frv list
  autoreconf -fiv
}

build() {
  cd ${pkgbase}-${pkgver}
  
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  PYTHON=/usr/bin/python3.exe ../${pkgname}-${pkgver}/configure \
    --prefix=/usr \
    --disable-dependency-tracking \
    --enable-shared \
    --enable-static \
    --enable-man \
    --enable-builtin=libidn2 \
    --enable-runtime=libidn2 \
    --with-psl-file=/usr/share/publicsuffix/public_suffix_list.dat \
    --with-psl-testfile=/usr/share/publicsuffix/test_psl.txt
  LC_CTYPE=en_US.UTF-8 make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make check
}

package_libpsl() {
 pkgdesc="Public Suffix List library (runtime)"
  depends=('libxslt' 'libidn2' 'libunistring')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/* ${pkgdir}/usr/bin/
  mkdir -p ${pkgdir}/usr/share/man
  cp -rf ${srcdir}/dest/usr/share/man/man1 ${pkgdir}/usr/share/man/
  install -Dm 644 ${srcdir}/${pkgbase}-${pkgver}/COPYING -t "${pkgdir}/usr/share/licenses/${pkgname}"

}

package_libpsl-devel() {
  pkgdesc="libpsl headers and libraries"
  options=('staticlibs')
  depends=("libpsl=${pkgver}" 'libxslt' 'libidn2-devel' 'libunistring')
  groups=('development')

  mkdir -p ${pkgdir}/usr/share/man/man3
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/

}

# vim: ts=2 sw=2 et:
